<?php
class InfocategoryAction extends CommonAction {
	function index(){
		$this->listmap.=" and pid=0";
        parent::_list();
		$Category=D('Infocategory');
		$list=$this->list;
		foreach($list as $k=>$v){
			$list[$k]['sub']=$Category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function delete(){
		if($this->_get('id')){
			$map['pid']=$this->_get('id');
			$re=$this->dao->where($map)->find();
			if($re){
				$this->error('该分类包含了子类,请先删除子类');
			}else{
				$all=$this->dao->where('id in('.$this->_get('id').')')->select();
				foreach($all as $k=>$v){
					//删除图片
					$this->delImgBypath($v['img_url']);
				}
				parent::delete();
			}
		}
	}
	public function add(){
		$module=M('Module');
		$pid_list=$this->dao->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		$category=D('Infocategory');
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id'],$this->siteid);
		}
		
		
		$this->assign('pid_list',$pid_list);
		$this->module_list=$module->select();
		if($this->_get('id')){
			$this->assign('id',$this->_get('id'));
		}
		parent::add();
	}
	
	public function insert(){
		$_POST['site_id']=$this->siteid;
		parent::insert();
	}
	function indextree(){
		$pid = $this->_get('pid') ? $this->_get('pid') : 0;
		$this->listmap.=" and pid=".$pid;
		$this->listorder = 'sort';
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and (title like '%{$keywords}%')";
			$this->keywords=$keywords;
		}
		
		$this->initView();
        parent::_list(); 
		$list=$this->list;
		$newsdb = M('Info');
		foreach($list as $k=>$v){
			$list[$k]['atriclecount'] = $newsdb -> where(array('infocategory_id' => $v['id'], 'site_id' => $this->siteid)) -> count();
		}
		$this->assign('list',$list);
		$str = $this->fetch();
		$list = $this->view -> get('list');
		foreach($list as $k => $v){
			$str = str_replace('<!--sub#'.$v['id'].'-->', $this->treehtml($v['id']), $str);
		}
		
		$list = $_GET['list'];
		$empty = 1;
		$statu=$list?1:0;
		if($this->get('list')){
			$empty = 0;
		}
		
		die(json_encode(array('empty' => $empty,'tbody'=>$str,'dump'=>$_REQUEST,'statu'=>$statu, 'pages' => $this-> view -> get('pages'))));
	}
	public function tree(){
		$map['pid'] = 0;
		if($this->_get('pid')){
			$map['pid'] = $this->_get('pid');
		}
		$map['site_id'] = __SITEID__;
		$list = $this->dao -> where($map) -> Field('title as name,id') -> select();
		foreach($list as $k => $v){
			//查找有无下级
			$sub = $this-> dao -> where(array('site_id' => __SITEID__, 'pid' => $v['id'])) -> find();
			$list[$k]['type'] = $sub ? 'folder' : 'item';
			$list[$k]['additionalParameters'] = array('id' => $v['id']);
		}
		echo json_encode($list);
	}
	private function treehtml($pid = 0){
		$list = $this->dao -> where(array('site_id' => __SITEID__, 'pid' => $pid)) -> order('sort') -> select();
		if($list){
			$newsdb = M('News');
			foreach($list as $k=>$v){
				$list[$k]['atriclecount'] = $newsdb -> where(array('category_id' => $v['id'], 'site_id' => $this->siteid)) -> count();
			}
			$this->assign('list', $list);
			$curstr = $this->fetch('indextree');
			foreach($this->list as $k => $v){
			    $curstr = str_replace('<!--sub#'.$v['id'].'-->', $this->treehtml($v['id']), $curstr);
		    }
			return $curstr; 
		}
	}
	
	private function parsesortdata($arr = array(), $ret = array(), $pid = 0){
		$pid1 = $pid;
		foreach($arr as $k => $v){
			$p['pid'] = $pid1;
			$p['id'] = $v['id'];
			$ret[] = $p;
			if(isset($v['children'])){
				$pid = $v['id'];
				$ret = $this->parsesortdata($v['children'], $ret, $pid);
			}
		}
		return $ret;
	}
	
	public function updatesort($data){
		$data = $this->parsesortdata(json_decode($_POST['data'], true));
		foreach($data as $k => $v){
			$map['id'] = $v['id'];
			$map['site_id'] = $this->siteid;
			$data['sort'] = $k + 1; 
			$data['pid'] = $v['pid'];
			$this->dao -> where($map) -> save($data);
		}
	}
	public function edit(){
		$pid_list=$this->dao->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		$category=D('Infocategory');
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('pid_list',$pid_list);
		parent::edit(); 
	}
	
	//重写更新方法,防止用户选择自己作为父分类
	public function update(){
		if($this->_post('pid')==$this->_post('id')){
			$this->error('您不能选择当前分类作为父类');
		}else{
			//不能重复
			parent::update();
		}
	}
}

?>